package vaani.internal.purple;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author sourcemorph
 */
public class BuddyListHandler {

    private static File blist = new File(System.getProperty("user.home") + "/.purple/blist.xml");
    public static AccountList accounts;
    private static List<Result> queryResult;
    /*AccountList is a sub class of LinkedList<InstantMessageAccount>*/

    static {
        readFile();
    }
    
    @SuppressWarnings("empty-statement")
    public static void readFile() {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        accounts = new AccountList();
        try {
            InputStream stream = new FileInputStream(blist);
            XMLEventReader eventReader = inputFactory.createXMLEventReader(stream);
            while(eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                //System.out.println(event.toString());
                if(event.isStartElement() && event.toString().equalsIgnoreCase("<contact>")) {
                    eventReader.nextEvent();
                    String eventString = eventReader.nextEvent().toString();
                    int indexEqualTo = eventString.indexOf("=") + 2;
                    //System.out.println(eventString + " " + indexEqualTo);
                    String buddyAccount = eventString.substring(indexEqualTo, eventString.indexOf("\'",indexEqualTo));
                    indexEqualTo = eventString.lastIndexOf("=") + 2;
                    String buddyProtocol = eventString.substring(indexEqualTo, eventString.indexOf("\'",indexEqualTo));
                    //System.out.println(buddyAccount + " " + buddyProtocol);
                    eventReader.nextEvent();
                    if(!accounts.contains(buddyAccount, buddyProtocol)) {
                        accounts.add(new InstantMessageAccount(buddyAccount, buddyProtocol));
                    }
                    while(!eventReader.nextEvent().toString().equals("<name>"));
                    String buddyName = eventReader.nextEvent().toString();
                    String buddyAlias = null;
                    eventReader.nextEvent();
                    eventReader.nextEvent();
                    if(eventReader.nextEvent().toString().equals("<alias>")) {
                        buddyAlias = eventReader.nextEvent().toString();
                    }
                    Buddy newBuddy = new Buddy(buddyName, buddyAlias);
                    accounts.get(accounts.indexOf(buddyAccount, buddyProtocol)).addBuddy(newBuddy);
                }
            }
        } catch(FileNotFoundException ex) {
            ex.printStackTrace();
        } catch(XMLStreamException ex) {
            ex.printStackTrace();
        }
    }

    public static List<Result> findBuddies(List<String> query) {
        for(String a : query) {
            System.out.println("query:" + a);
        }
        queryResult = new LinkedList<Result>();
        for(InstantMessageAccount account : accounts) {
            List<Buddy> tempBuddyList = account.getBuddies();
            for(Buddy buddy : tempBuddyList) {
                //System.out.println(buddy.getName());
                boolean flag = true;
                for(String queryItem : query) {
                    queryItem = queryItem.toLowerCase();
                    //System.out.println(queryItem);
                    /*first search in the account name
                     * and then in the alias*/
                    if(!buddy.getName().toLowerCase().contains(queryItem)) {
                        if(buddy.getAlias() == null || !buddy.getAlias().toLowerCase().contains(queryItem)) {
                            if(buddy.getAlias() != null) {
                                //System.out.println(buddy.getAlias());
                            }
                            flag = false;
                            break;
                        }
                    }
                }
                if(flag) {
                    queryResult.add(new Result(account, buddy));
                }
            }
        }
        return queryResult;
    }

    public static boolean matchProtocol(String query) {
        for(InstantMessageAccount account : accounts) {
            if(account.getProtocol().contains(query)) {
                return true;
            }
        }
        return false;
    }

    public static List<Buddy> getAllBuddies() {
        List<Buddy> buddyList = new LinkedList<Buddy>();
        for(InstantMessageAccount account : accounts) {
            buddyList.addAll(account.getBuddies());
        }
        return buddyList;
    }

    public static List<String> getAllProtocols() {
        List<String> protocols = new LinkedList<String>();
        for(InstantMessageAccount account : accounts) {
            protocols.add(account.getProtocol());
        }
        return protocols;
    }

    
    /*public static void main(String [] args) {
        Vector<String> temp = new Vector<String>();
        temp.add("mohit");
        Vector<Result> tempResult = findBuddies(temp);
        for(Result r : tempResult) {
            System.out.println(r.getBuddy().getName());
        }
    }*/
}
